package com.happok.onlineclass.usermanagement.authentication.config;

import cn.hutool.json.JSONObject;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/7/3 17:07
 */
@Component
public class CustomSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
        HttpServletResponse response = sessionInformationExpiredEvent.getResponse();
        response.setContentType("application/json;charset=utf-8");
        JSONObject jsonObject = new JSONObject(true);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        jsonObject.set("code","40102");
        jsonObject.set("msg","此账号在别的地方登录");
        response.getWriter().write(jsonObject.toString());
        response.flushBuffer();
    }
}